using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using MapEditor;
using System.Configuration;

namespace MapEditor
{
    public partial class mainForm : Form
    {
        public bool MovingPicture;
        public int PreviousMouseX;
        public int PreviousMouseY;

        //defines the main map image bitmap
        public Image mapImage;
        public object mapTerrain;
        public object myMap;



        public mainForm()
        {
            InitializeComponent();
            myMap = new Map("");


        }

         






        private void btnSave_Click(object sender, EventArgs e)
        {
            bool MapSaved;


            string SaveLocation = this.txtSaveLocation.Text;
            string MapName = this.txtMapName.Text;
            Map ccMap = new Map(MapName);


            ccMap = (Map)myMap;


            MapSaved = ccMap.CreateMapTiles(SaveLocation);

            if (!MapSaved)
            {
                MessageBox.Show("Could not save map.");
            }
            else
            {
                MessageBox.Show("Map saved to : " + SaveLocation + MapName);

            }

            
        }




        //Mouse picture controls
        private void picMap_OnMouseDown(object sender, MouseEventArgs e)
        {
            MovingPicture = true;


        }

        private void picMap_OnMouseUp(object sender, MouseEventArgs e)
        {
            MovingPicture = false;
        }

        private void picMap_OnMouseMove(object sender, MouseEventArgs e)
        {
            if (MovingPicture)
            {

                lblMouseX.Text = Convert.ToString(e.X);
                lblMouseY.Text = Convert.ToString(e.Y);

                //get the element for this x and y

                int ElementID;
                int ElevationValue;
                //Terrain myTerrain;
                //myTerrain = (Terrain)mapTerrain;
                Map ccMap = new Map("");
                ccMap = (Map)myMap;

                ElementID = (int)ccMap.TerrainArray.GetValue(((hScrollBar1.Value+e.X )/ 10), ((vScrollBar1.Value+ e.Y) / 10));
                lblElementType.Text = (string)ccMap.TerrainElement(ElementID);
                ElevationValue = (int)ccMap.ElevationArray.GetValue(((hScrollBar1.Value + e.X) / 10), ((vScrollBar1.Value + e.Y) / 10));
                lblElevation.Text = (string)"" +ElevationValue;


            }

        }

        private void mainForm_Load(object sender, EventArgs e)
        {

        }


        public void DisplayScrollBars()
        {
            // If the image is wider than the PictureBox, show the HScrollBar.
            if (picMap.Width > picMap.Image.Width - this.vScrollBar1.Width)
            {
                hScrollBar1.Visible = false;
            }
            else
            {
                hScrollBar1.Visible = true;
            }

            // If the image is taller than the PictureBox, show the VScrollBar.
            if (picMap.Height >
                picMap.Image.Height - this.hScrollBar1.Height)
            {
                vScrollBar1.Visible = false;
            }
            else
            {
                vScrollBar1.Visible = true;
            }
        }

        private void HandleScroll(Object sender, ScrollEventArgs se)
        {
            /* Create a graphics object and draw a portion 
               of the image in the PictureBox. */
            Graphics g = picMap.CreateGraphics();

            g.DrawImage(picMap.Image,
              new Rectangle(0, 0, picMap.Right,
              picMap.Bottom),
              new Rectangle(hScrollBar1.Value, vScrollBar1.Value,
              picMap.Right,
              picMap.Bottom),
              GraphicsUnit.Pixel);

            picMap.Update();
            lblMapX.Text = Convert.ToString(hScrollBar1.Value);
            lblMapY.Text = Convert.ToString(vScrollBar1.Value);

        }





        public void SetScrollBarValues() 
        {
           // Set the Maximum, Minimum, LargeChange and SmallChange properties.
           this.vScrollBar1.Minimum = 0;
           this.hScrollBar1.Minimum = 0;

           // If the offset does not make the Maximum less than zero, set its value. 
           if( (this.picMap.Image.Size.Width - picMap.ClientSize.Width) > 0)
           {
              this.hScrollBar1.Maximum = 
                  this.picMap.Image.Size.Width - picMap.ClientSize.Width;
           }
           // If the VScrollBar is visible, adjust the Maximum of the 
           // HSCrollBar to account for the width of the VScrollBar.  
           //if(this.vScrollBar1.Visible)
           //{
           //   this.hScrollBar1.Maximum += this.vScrollBar1.Width;
           //}
           this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum / 10;
           this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum / 20;

           // Adjust the Maximum value to make the raw Maximum value 
           // attainable by user interaction.
           this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange;
             
           // If the offset does not make the Maximum less than zero, set its value.    
           if( (this.picMap.Image.Size.Height - picMap.ClientSize.Height) > 0)
           {
              this.vScrollBar1.Maximum = 
                  this.picMap.Image.Size.Height - picMap.ClientSize.Height;
           }

           // If the HScrollBar is visible, adjust the Maximum of the 
           // VSCrollBar to account for the width of the HScrollBar.
           //if(this.hScrollBar1.Visible)
           //{
           //   this.vScrollBar1.Maximum += this.hScrollBar1.Height;
           //}
           this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum / 10;
           this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum / 20;
           
            // Adjust the Maximum value to make the raw Maximum value 
           // attainable by user interaction.
           this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange;
           }



        private void picMap_Click(object sender, EventArgs e)
        {

        }

        private void btnConvert_Click(object sender, EventArgs e)
        {

            Map ccMap = new Map("");
            ccMap = (Map)myMap;
            ccMap.Name = txtMapName.Text;

            string mapFile = txtFileLocation.Text + txtMapName.Text;
            //load the roof information
            ccMap.Roofs = (Roofs)CC.LoadRoofInformation(mapFile + ".RFM");
            //load and convert the BGM
            ccMap.BaseImage = CC.ConvertCCMapToIMage(mapFile + ".BGM");
            //load and convert the OVM
            ccMap.OverviewImage = CC.ConvertCCMapToIMage(mapFile + ".OVM");
            //load and convert the MMM
            ccMap.ThumbnailImage = CC.ConvertCCMapToIMage(mapFile + ".MMM");
            //Load and convert the Elevations
            ccMap.ElevationArray = CC.LoadTerrain(mapFile + ".TXT", ref ccMap, CC.TerrainType.Elevation);
            //Load and convert the Elements
            ccMap.TerrainArray = CC.LoadTerrain(mapFile + ".TXT", ref ccMap, CC.TerrainType.Terrain);
            //load the terrain element descriptions to make sense of the terrain array
            ccMap.TerrainElements = (TerrainElements.Terrain[])CC.LoadTerrainElementsFromFile("C:\\Graphics\\Maps\\Data\\Elements.xml");



            //load the image into the picturebox and load the thumbnail

            //this.picMap.ImageLocation  = txtFileLocation.Text;
            this.picMap.Image = ccMap.BaseImage;
            this.picThumbnail.Image = ccMap.ThumbnailImage;

            //load the dimensions of the image
            lblIMageXPix.Text = ccMap.Width.ToString();
            lblIMageYPix.Text = ccMap.Height.ToString();

            //display the scrollbars, if needed
            this.DisplayScrollBars();
            this.SetScrollBarValues();
            this.Refresh();


            //Complete!
            MessageBox.Show("Map:" + ccMap.Name + " loaded.");


        }





  






    }
}